<% content_for(:for_javascript) do %>
<% jscomp('app/project/list_calendar, app/task/dlg_index, app/projectuser/dlg_select, app/milestone/dlg_index, app/event/dlg_index') %>
<% end %>

<% content_for(:for_stylesheet) do %>
<%- csscomp('app/list_calendar') -%>
<% end %>

<table class="mainpage_content_table" border="0" cellpadding="0" cellspacing="0" style="width:100%;height:100%;">
  <tr>
    <td valign="top">
        <table class="mainpage_content_table" border="0" cellpadding="0" cellspacing="0" style="width:100%;height:100%;">
          <tr>
            <td class="mainpage_content_head">
            <table style="width:100%;">
              <tr>
                <td style="vertical-align:top;">
                  <span>Calendar</span>
                  <form name="navigation" id="navigation" method="post" style="display:inline;">
                  <%= hidden_field_tag :base_id  %>
                  <%= hidden_field_tag :class_word1  %>
                  <%= hidden_field_tag :class_word2  %>
                  <%= hidden_field_tag :class_word3  %>
                  </form>
                  <span id="remote_project_name" style="visibility:hidden;"><%=h @project.project_name %></span>
                  <span id="remote_form"><%= hidden_field( :project, :id, :value=>@project.id, :id=>"remote_project_id" ) %></span>
                </td>
                <td style="vertical-align:bottom;width:140px;display:none;">
                    <%= select_task_kbn_tag() %>
                </td>
                <td style="vertical-align:bottom;width:220px;display:none;">
                    <%= select_for_users(:calendar_filter, :client_user_id, @project.id,
                                        {:include_all => true, :selected=>""}, {:style=>"width:200px;", :onChange=>"listCalendar.fncCalendarFilterUser(this);"} ) %>
                </td>
                <td style="vertical-align:bottom;width:120px;">
                    <span style="font-weight:normal;">&nbsp;｜&nbsp;</span>
                    <%= link_to_function( image_tag('/images/btn/btn_task.gif',{:border => 0, :align=>"absmiddle", :alt=>app_localized_message(:label, :add_task)}) , :id=>'list_calendar_btn_add_task' ) %>
                    <%= link_to_function( image_tag('/images/btn/btn_milestone.gif',{:border => 0, :align=>"absmiddle", :alt=>app_localized_message(:label, :add_milestone)}), :id=>'list_calendar_btn_add_milestone' ) %>
                    <%= link_to_function( image_tag('/images/btn/btn_event.gif',{:border => 0, :align=>"absmiddle", :alt=>app_localized_message(:label, :add_event)}), :id=>'list_calendar_btn_add_event' ) %>
                </td>
              </tr>
            </table>
            </td>
          </tr>
          <tr>
            <td class="mainpage_content_content" style="vertical-align:top;">
              <div id="pro_list_calendar_body" class="pro_list_calendar_body" style="width:100%;">

<table class="list_calendar_table" border="0" style="width:100%;" cellspacing="0" cellpadding="0">
  <tr>
    <td class="list_calendar_cell list_calendar_h list_calendar_cell_left sun" width="14.2857%">日</td>
    <td class="list_calendar_cell list_calendar_h" width="14.2857%">月</td>
    <td class="list_calendar_cell list_calendar_h" width="14.2857%">火</td>
    <td class="list_calendar_cell list_calendar_h" width="14.2857%">水</td>
    <td class="list_calendar_cell list_calendar_h" width="14.2857%">木</td>
    <td class="list_calendar_cell list_calendar_h" width="14.2857%">金</td>
    <td class="list_calendar_cell list_calendar_h sat" width="14.2857%">土</td>
    <td id = "margin" class="list_calendar_cell list_calendar_h" width="1%" style="width:1%;"><%= image_tag("s.gif", {:style=>"width:18px;"}) %></td>
  </tr>
  <tr>
    <td colspan="8" style="vertical-align:top;">

<div id="calendar_wrapper" style="width:100%;">
<div id="calendar_wrapper_child" style="height:500px;overflow:auto!important;">
<table id="calendar_table" border="0" style="width:100%;" cellspacing="0" cellpadding="0">
<%-
   start_date = @project.start_date
   end_date = @project.delivery_date
   
   today_date = Date.today
   start_date = Date.new( today_date.year, today_date.month, 1 ) if start_date.nil? 
   end_date = Date.new( today_date.year, today_date.month+1, 1 ) - 1 if end_date.nil? 

   
   sdate = Time.mktime( start_date.year, start_date.month, 1 )
   sdate = sdate - ( sdate.wday * 24 * 60 * 60 )

  logger.debug end_date
   tmp_edate = end_date >> 1
   edate = Time.mktime( tmp_edate.year, tmp_edate.month, 1 ) - (24*60*60)
   edate = edate + ( (6 - edate.wday ) * 24 * 60 * 60 )
   weeks = ( ( edate + ( 1 * 24 * 60 * 60 ) - sdate ) / 60 / 60 / 24 / 7  )
-%>
<%- for week in 0..(weeks-1) -%>
  <tr>
  <%- for day in 0..6 
        if day == 0 
            classstr = "list_calendar_cell list_calendar_cell_left sun" ;
        elsif day == 6
            classstr = "list_calendar_cell sat" ;
        else
            classstr = "list_calendar_cell " ;
        end
        
        curdate = sdate + (week * 7 * 60 * 60 * 24 ) + (day * 60 * 60 * 24 )
        
        if( curdate.day == 1 || (week == 0 && day == 0 ) )
            datestr = curdate.strftime('%m'+ app_localized_message("label", "month") + '%d') ;
        else
            datestr = curdate.strftime('%d') ;
        end
  -%>
    <td width="14.2857%" id="cell_<%= curdate.strftime('%Y-%m-%d') %>" class="<%= classstr %>"><div class="datearea" style="width:100%;text-align:right;"><%= datestr %></div><div id="area_<%= curdate.strftime('%Y-%m-%d') %>"></div></td>
  <%- end -%>
  </tr>
<%- end -%>
</table>

<%-

    for projectcomp in @project.dat_projectcomps
        sdate = nil
        edate = nil
        color = ""
        item = ""
        case projectcomp.task_kbn
        when 1
            sdate = projectcomp.dat_task.start_date
            edate = projectcomp.dat_task.end_date
            color = "#99CCCC"
            item = h(projectcomp.item_name)
            userids = projectcomp.dat_task.dat_taskusers.map{ |tu|  
                if tu.dat_projectuser.nil?
                    ""
                else
                    tu.dat_projectuser.id
                end
            }
        when 2
            sdate = projectcomp.dat_milestone.mils_date
            edate = projectcomp.dat_milestone.mils_date
            color = "#FFCC99"
            item = h(projectcomp.item_name)
        when 3
            sdate = projectcomp.dat_event.start_date
            edate = projectcomp.dat_event.start_date
            color = "#FFFFFF"
            item = "" + (projectcomp.dat_event.start_time.nil? ? "" : projectcomp.dat_event.start_time.strftime('%H:%M') ) + "&nbsp;" + h(projectcomp.item_name)
            userids = projectcomp.dat_event.dat_eventusers.map{ |eu|  
                if eu.dat_projectuser.nil?
                    ""
                else
                    eu.dat_projectuser.id
                end
            }
        end
        next if sdate.nil? || edate.nil? 

        divsdate = sdate
        divedate = sdate

        while edate >= divedate

            weeklastday = divsdate + (6 - divsdate.wday)

            if weeklastday > edate
                divedate = edate
            else
                divedate = weeklastday
            end

-%>
  <div class="list_calendar_items" data="<%=h projectcomp.to_json %>" userids="<%=h userids.to_json %>" sdate="<%=h divsdate.strftime('%Y-%m-%d') %>" edate="<%=h divedate.strftime('%Y-%m-%d') %>" style="display:none;position:relative;height:20px;width:100%;color:#000000;"><div style="height:1px;line-height:0px;font-size:0px;margin:0px 3px;background-color:<%= color %>;"></div><div style="height:1px;line-height:0px;font-size:0px;margin:0px 1px;background-color:<%= color %>;"></div><div class="list_calendar_items_body" style="height:14px;background-color:<%= color %>;padding-left:3px;cursor:pointer;"><%= item %></div><div style="height:1px;line-height:0px;font-size:0px;margin:0px 1px;background-color:<%= color %>;"></div><div style="height:1px;line-height:0px;font-size:0px;margin:0px 3px;background-color:<%= color %>;"></div></div>
<%-
            if divedate < edate
                divsdate = divedate + 1
            else
                divedate = divedate + 1
            end
        end 
-%>
<%- end -%>

</div>
</div>

    </td>
  </tr>
</table>

              </div>
            </td>
          </tr>
        </table>
    </td>
  </tr>
</table>

<!-- タスク追加ダイアログ start -->
<%= render :partial=>"task/dlg_index" %>
<!-- タスク追加ダイアログ end -->

<!-- マイルストーン追加ダイアログ start -->
<%= render :partial=>"milestone/dlg_index" %>
<!-- マイルストーン追加ダイアログ end -->

<!-- イベント追加ダイアログ start -->
<%= render :partial=>"event/dlg_index" %>
<!-- イベント追加ダイアログ end -->

<!-- 担当者選択ダイアログ start -->
<%= render :partial=>"projectuser/dlg_select" %>
<!-- 担当者選択ダイアログ end -->
